package com.song.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.song.server.entity.MenuRole;
import com.song.server.mapper.MenuRoleMapper;
import com.song.server.service.MenuRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.song.server.vo.RespBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author inkolopolookni
 * @since 2021-10-19
 */
@Service
public class MenuRoleServiceImpl extends ServiceImpl<MenuRoleMapper, MenuRole> implements MenuRoleService {

    @Autowired
    private MenuRoleMapper menuRoleMapper;

    @Override
    @Transactional
    public RespBean updateMenuRole(Integer rid, Integer[] mids) {
        // 1. 先删除rid下所有的mid
        menuRoleMapper.delete(new QueryWrapper<MenuRole>().eq("rid", rid));

        // 2. 判断传入的菜单id集合是否为空
        if (null == mids || 0 == mids.length) {
            return RespBean.success("更新成功！");
        }

        // 3. 将传入的菜单id集合写入数据库
        Integer result = menuRoleMapper.insertMids(rid, mids);
        if (result == mids.length) {
            return RespBean.success("更新成功！");
        }
        return RespBean.error("更新失败！");
    }
}
